package cn.wolfcode.exception;

import cn.wolfcode.qo.JsonResult;
import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.HandlerMethod;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

@ControllerAdvice
public class MyExceptionHandler {


    @ExceptionHandler(Exception.class)
    public String handler(Exception e, HandlerMethod method, HttpServletResponse response) throws IOException {
        e.printStackTrace();
        if(method.hasMethodAnnotation(ResponseBody.class)){
                response.getWriter().println(JSON.toJSONString(new JsonResult(false,e.getMessage())));
        }
        return "/common/error";
    }

    @ExceptionHandler(LoginException.class)
    @ResponseBody
    public JsonResult loginException(LoginException e){
     return  new JsonResult(false,e.getMessage());
    }
}
